//
//  YFGeneralViewController.m
//  SquashSpark
//
//  Created by Coollang on 2017/12/5.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFGeneralViewController.h"
#import <YYKit/YYKit.h>
#import "YFUserDefaults.h"

@interface YFGeneralViewController ()

@property (nonatomic, strong) UITableView *tabelView;
@property (weak, nonatomic) IBOutlet YYLabel *bottomTipLabel;
@end

@implementation YFGeneralViewController {
    BOOL _isChangeGeneralUnit; // 是否正在切换单位
}

- (void)loadView {
    self.tabelView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tabelView.backgroundColor = kSeparationLineColor;
    self.tabelView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    self.view = self.tabelView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kSeparationLineColor;
    self.tabelView.sectionFooterHeight = 0;
    [self loadItemData];
    [self creatTableViewFooter];
}

- (void)loadItemData {
    UserGroup *group0 = [[UserGroup alloc] init];
    UserSaveItem *item0_0 = [UserSaveItem itemWithTitle:@"Imperial" andSubTitle:@"ft,in,mph,lbs" withSelect:![YFUserDefaults generalMetricSystemUnit]];
    __weak typeof(self) weakSelf = self;
    [item0_0 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        [weakSelf selectGeneralWithBritishUnit:YES];
    }];
    [group0.items addObjectsFromArray:@[item0_0]];
    
    UserGroup *group1 = [[UserGroup alloc] init];
    UserSaveItem *item1_0 = [UserSaveItem itemWithTitle:@"Metric" andSubTitle:@"m,cm,kph,kg" withSelect:[YFUserDefaults generalMetricSystemUnit]];
    [item1_0 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        [weakSelf selectGeneralWithBritishUnit:NO];
    }];
    [group1.items addObject:item1_0];
    [self.groups addObjectsFromArray:@[group0,group1]];
}

// 是否是选择了英制单位
- (void)selectGeneralWithBritishUnit:(BOOL)isBritishUnit {
    if (_isChangeGeneralUnit == NO) {
        _isChangeGeneralUnit = YES;
        UserSaveItem *itemB = [[self.groups.firstObject items] objectAtIndex:0];
        UserSaveItem *itemMetric = [[self.groups.lastObject items] objectAtIndex:0];
        itemB.isSelected = isBritishUnit;
        itemMetric.isSelected = !isBritishUnit;
        [YFUserDefaults setGeneralMetricSystemUnit:!isBritishUnit];
        [self.hud showWithString:@"" maskType:WSProgressHUDMaskTypeGradient];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            _isChangeGeneralUnit = NO;
        });
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView withStyle:UITableViewCellStyleValue1];
    
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    cell.item = item;
    [cell showSeparationBottomLine:NO];
    cell.detailLabelHOffset = -22;
    cell.textLabel.font = kOpenSansFont(OpenSansBold, 16);
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kAutoHei(67);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.00000001;
    }
    return 10;
}
- (void)creatTableViewFooter {
    YYLabel *label = [[YYLabel alloc] init];
    label.numberOfLines = 0;
    self.bottomTipLabel = label;
    NSString *labelText = @"Changing your unit of measure affects the units displayed in the app and the content you share";
    //@"After setting up the racket parameters,please re calibrate the equipment";
    label.text = labelText;
    [self configueryy_Label:label string:labelText hightlightStr:@""];
    CGSize labelSzie = [label sizeThatFits:CGSizeMake(kYFScreenWidth - 32, MAXFLOAT)];
    label.frame = CGRectMake(16, 26, labelSzie.width, labelSzie.height + 4);
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, CGRectGetMaxY(label.frame))];
    [footerView addSubview:label];
    self.tabelView.tableFooterView = footerView;
}

- (void)configueryy_Label:(YYLabel *)yy_label string:(NSString *)joinString hightlightStr:(NSString *)highlightStr {
    
    yy_label.displaysAsynchronously = YES;
    
    // 计算文本内容的大小
    NSRange joinrange = [joinString rangeOfString:highlightStr];
    NSMutableAttributedString *joinText = [[NSMutableAttributedString alloc] initWithString:joinString];
    joinText.font = kOpenSansFont(OpenSansRegular, 13);
    joinText.color = kTLightGray999;
    
    joinText.lineSpacing = 8;
    [joinText setUnderlineStyle:NSUnderlineStyleSingle];
    [joinText setUnderlineColor:[UIColor clearColor]];
    joinText.baselineOffset = @2;
    joinText.alignment = NSTextAlignmentLeft;
   
    [joinText setUnderlineColor:kTWhiteText range:joinrange];
    [yy_label sizeToFit];
    
    [joinText setTextHighlightRange:joinrange color:kTWhiteText backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
    }];
    yy_label.attributedText = joinText;
}



@end
